﻿using AutoMapper;
using Business.ReadAPI.Application.Immunities;
using Common.Domain;
using Common.Infrastructure.Repository;
using Common.Services;
using MediatR;

namespace Business.ReadAPI.Application.Images
{
    public class ImageQueryRequestHandler : IRequestHandler<ImageQueryRequestModel, APIResult>
    {
        private readonly IBaseRepository<Image> imageRepository;
        private readonly IMapper mapper;
        public ImageQueryRequestHandler(IBaseRepository<Image> imageRepository, IMapper mapper)
        {
            this.imageRepository = imageRepository;
            this.mapper = mapper;
        }
        public Task<APIResult> Handle(ImageQueryRequestModel request, CancellationToken cancellationToken)
        {
            APIResult result = new APIResult();
            result.Code = EnumCode.SearchSuccess;
            result.Msg = "查询成功";

            var list = imageRepository.GetAll().Where(x=>x.ProductCode==request.ProductCode && x.IsDel==false);
            List<ImageQueryResponseModel> responseModels = mapper.Map<List<ImageQueryResponseModel>>(list);
            result.Data = responseModels;

            return Task.FromResult(result);
        }
    }
    public class ImageQueryRequestModel:IRequest<APIResult>
    {
        public string ProductCode { get; set; }
    }
    public class ImageQueryResponseModel
    {
        public string Url { get; set; }
    }
}
